package com.xuan.controller;

import com.xuan.api.UserRemoteApi;
import com.xuan.model.entity.User;
import com.xuan.model.respons.ResultBody;
import com.xuan.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController implements UserRemoteApi {

    @Autowired
    private IUserService userService;


    @GetMapping("/register/chekUsername/{username}")
    public ResultBody chekUsername(@PathVariable("username") String username) {
        ResultBody resultBody = userService.chekUsername(username);
        log.info(resultBody.getMessage());
        return resultBody;
    }

    @PostMapping("/register/savaUser")
    public ResultBody savaUser(User user) {

        log.info("获得参数对象=============="+user);
        return userService.savaUser(user);
    }

    @GetMapping("/getAll")
    public ResultBody getAll() {
        return new ResultBody().OK().data(userService.list());
    }

    @GetMapping("/deleteByUsername/{username}")
    public ResultBody deleteByUsername(@PathVariable("username") String username) {

        return userService.deleteByUsername(username);
    }

    @GetMapping("/chekMobile/{mobile}")
    public boolean chekMobile(@PathVariable("mobile") String mobile) {
        List<User> users = userService.query().eq("mobile", mobile).list();
        return users.size() <= 0 ? true : false;
    }

    @PostMapping("/resetPassword")
    public ResultBody resetPassword(User user) {
        return userService.resetPassword(user);
    }

}
